#include "player.h"
#include "physicsmanager.h"

Player::Player(Ogre::String p_Name, Ogre::SceneManager* p_SceneManager, Ogre::String p_ResourceGroupName)
{
	m_Name = p_Name;
	m_SceneManager = p_SceneManager;
    m_ResourceGroupName = p_ResourceGroupName;

	m_Score = 0;
	m_Difficulty = 1;

	m_dPhi = Ogre::Degree(0.5);
	m_Velocity = Ogre::Vector3::UNIT_X;

	m_dPhiY = Ogre::Degree(0);
	m_dPhiZ = Ogre::Degree(0);

	CreateNodes();
    CreateEntity();
}

void Player::CreateEntity()
{
    m_Entity = m_SceneManager->createEntity(m_Name + "Entity", "PlayerCollObj.mesh", m_ResourceGroupName);

    // Create endoscope head and attach.
    m_EntityNode = m_ObjectRootNode->createChildSceneNode("EntityNode");
    m_EntityNode->attachObject(m_Entity);

    PhysicsManager::GetInstance()->AddMovableEntity(m_Entity);

    m_EntityTail = m_SceneManager->createEntity(m_Name + "EntityTail", "PlayerRest.mesh", m_ResourceGroupName);

    // Create endoscope head and attach.
    m_EntityNode->attachObject(m_EntityTail);
}

void Player::CreateNodes()
{
	m_ObjectRootNode = m_SceneManager->getRootSceneNode()->createChildSceneNode("PlayerNode");
	m_ObjectRootNode->setPosition(-250,0,0);

	CreateEntityNode();
	CreateLightNode();
	CreateSpotLightNode();
	CreateCameraNode();
}

void Player::CreateEntityNode()
{
}

void Player::CreateLightNode()
{
	// Create Light
	m_Light = m_SceneManager->createLight("MainLight");

    // Create a Lightnode and attach light
	m_LightNode = m_ObjectRootNode->createChildSceneNode("LightNode");
	m_LightNode->attachObject(m_Light);

    // Modifiy light
	m_Light->setDiffuseColour(0.00, 0.00, 0.00);
    m_Light->setCastShadows(false);
    m_LightNode->setPosition(-40,0,0);
}

void Player::CreateSpotLightNode()
{
    // Create Light
    m_SpotLight = m_SceneManager->createLight("SpotLight");

    // Create a Lightnode and attach light
	m_SpotLightNode = m_ObjectRootNode->createChildSceneNode("SpotLightNode");
	m_SpotLightNode->attachObject(m_SpotLight);

    // Modifiy light
	m_SpotLightNode->setPosition(-300,0,0);
	m_SpotLight->setType(Ogre::Light::LT_SPOTLIGHT);
	m_SpotLight->setDirection(Ogre::Vector3(1,0,0));
	m_SpotLight->setDiffuseColour(1,1,1);
    //Width of the beams and core beam
	m_SpotLight->setSpotlightRange(Ogre::Degree(0), Ogre::Degree(15));
}

void Player::CreateCameraNode()
{
	m_CameraNode = m_ObjectRootNode->createChildSceneNode("PlayerCameraNode");

	// Create the camera
	m_Camera = m_SceneManager->createCamera("Camera");

	// Attach to player Node
	m_CameraNode->attachObject(m_Camera);

	// Position behind player
	m_CameraNode->setPosition(Ogre::Vector3(-50,0,0));

	// Look back along Z
	m_Camera->lookAt(Ogre::Vector3::UNIT_X);
	m_Camera->setNearClipDistance(0.1);
}

void Player::InvokeUp()
{
	m_dPhiZ += m_dPhi;
}

void Player::InvokeDown()
{
	m_dPhiZ -= m_dPhi;
}

void Player::InvokeLeft()
{
	m_dPhiY += m_dPhi;
}

void Player::InvokeRight()
{
	m_dPhiY -= m_dPhi;
}

void Player::Update()
{
	Move();
	m_Score += Ogre::Math::Floor(m_Difficulty * m_Difficulty);

	if (m_Score % 1000 == 0)
	{
		m_Difficulty += 0.01;
	}
}

void Player::Move()
{
	m_ObjectRootNode->rotate(Ogre::Vector3::UNIT_Z, m_dPhiZ);
	m_ObjectRootNode->rotate(Ogre::Vector3::UNIT_Y, m_dPhiY);
	m_Velocity = m_ObjectRootNode->getOrientation() * Ogre::Vector3::UNIT_X;


	m_ObjectRootNode->setPosition(m_ObjectRootNode->getPosition() + m_Difficulty * m_Velocity);

//	debug = "Length: " + std::to_string(m_Velocity.length()) +
//			"\nRoll: " + std::to_string(m_RootNode->getOrientation().getRoll().valueAngleUnits()) +
//			"\nYaw: " + std::to_string(m_RootNode->getOrientation().getYaw().valueAngleUnits()) +
//			"\nPitch: " + std::to_string(m_RootNode->getOrientation().getPitch().valueAngleUnits());

	//debug = Ogre::StringConverter::toString(m_RotZ) + "\n" + Ogre::StringConverter::toString(m_RotY);


}

Ogre::Camera* Player::GetCamera()
{
    return m_Camera;
}

Player::~Player()
{
    m_SceneManager->destroyEntity(m_Entity);
    m_SceneManager->destroyEntity(m_EntityTail);
    m_SceneManager->destroyLight(m_Light);
    m_SceneManager->destroyLight(m_SpotLight);
    m_SceneManager->destroyCamera(m_Camera);
    m_SceneManager->getRootSceneNode()->removeAndDestroyChild(m_ObjectRootNode->getName());

    m_Entity = 0;
    m_EntityTail = 0;
    // TO DO
    // PhysicsManager::GetInstance()->REMOVEENTITY
}
